<template>
  <div class="dialog-form">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="机构代码" prop="organCode">
        <el-input v-model="form.organCode" placeholder="请输入机构代码" />
      </el-form-item>
      <el-form-item label="卡IIN" prop="cardIin">
        <el-input v-model="form.cardIin" placeholder="请输入卡IIN" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getOrganwhitelist, addOrganwhitelist, updateOrganwhitelist } from "@/api/bus/organwhitelist"

const { proxy } = getCurrentInstance()
const emits = defineEmits(['cancel', 'confirm'])

const formRef = ref()
const form = ref({
  id: null,
  organCode: null,
  cardIin: null
})
const rules = {
  organCode: [
    { required: true, message: "机构代码不能为空", trigger: "blur" }
  ],
  cardIin: [
    { required: true, message: "卡IIN不能为空", trigger: "blur" }
  ],
}

const pageInit = (id) => {
  getOrganwhitelist(id).then(response => {
    form.value = response.data
  })
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  formRef.value.validate(valid => {
    if (valid) {
      if (form.value.id) {
        updateOrganwhitelist(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          emits('confirm')
        })
      } else {
        addOrganwhitelist(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>